# -*- coding: utf-8 -*-            
# @Time : 2023/5/21 16:34
# @Author  : lining
# @FileName: 二叉树最大深度_自顶向下.py
"""
思路2：自定向下，把深度作为一个全局变量,递归一层加1，在叶子处还原，要注意保护和还原现场
"""
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


node5 = TreeNode(7)
node6 = TreeNode(9)
node2 = TreeNode(8, node5, node6)
node8 = TreeNode(13)
node9 = TreeNode(0)
node10 = TreeNode(100)
node7 = TreeNode(11, node10)
node3 = TreeNode(12, node7, node8)
node4 = TreeNode(10, node2, node3)
deep = 1
tree_deep = 1

def find_deep(node):
    global deep
    global tree_deep
    if node is None:
        return
    deep = max(deep, tree_deep)
    # 进入节点+1
    tree_deep = tree_deep+1
    find_deep(node.left)
    # tree_deep = tree_deep+1
    # tree_deep = tree_deep-1
    find_deep(node.right)
    # 还原现场
    tree_deep = tree_deep-1
    return deep

print(find_deep(node3))


